package com.example.register_demo.home.LoginAndRegister

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.lifecycle.ViewModelProvider
import com.example.register_demo.home.HomeFragment
import com.example.remindme.HomePages.userDatabase.DatabaseViewModel
import com.example.remindme.R

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch


class LoginFragment : Fragment() {
    private lateinit var logBtn: Button
    private lateinit var accountEdit: EditText
    private lateinit var passEdit: EditText
    private lateinit var regBtn: Button
    private lateinit var loginVM: RegisterModel
    private lateinit var if_remember:CheckBox
    private lateinit var tv:TextView


    private lateinit var dataViewModel : DatabaseViewModel // 这里需要登录的数据库



    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false)

    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)


        // 获取数据的viewmodel


    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        val v=getView()
        if(v!==null)
        {
            logBtn=v.findViewById(R.id.loginBtn)
            regBtn=v.findViewById(R.id.registerBtn)
            accountEdit=v.findViewById(R.id.logAccount)
            passEdit=v.findViewById(R.id.logPassword)
            if_remember=v.findViewById(R.id.login_remember)

        }



        dataViewModel= ViewModelProvider(this,
                ViewModelProvider.AndroidViewModelFactory(requireActivity().application)).get(DatabaseViewModel::class.java)
        loginVM= ViewModelProvider(this).get(RegisterModel::class.java)


        logBtn.setOnClickListener{
            // 点击登录对登录合法性判断
            // 检查数据库内是否存在改账号，检查数据库内密码是否与输入一致\
            if(if_remember.isChecked)
            {
            // 如果点击了记住密码，则需要更新
                dataViewModel.is_remember=true
            }
            JudgeLogin()

        }
        regBtn.setOnClickListener{
            // 前往下一个 fragment进行，哇，fragment内可以杀掉自己的！！！！
           // navController.navigate(R.id.action_loginFragment2_to_registerFragment)

            val fragment= RegisterFragment()
            requireActivity().supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentcontainer,fragment,"register")
                .addToBackStack(null)
                .commit()



        }
        loginVM.loginEqual.observe(requireActivity(),{
            if(it)
            {

                Toast.makeText(requireActivity(),"Login successfully",Toast.LENGTH_SHORT).show()
                val fragment= HomeFragment()
                requireActivity().supportFragmentManager.beginTransaction()
                        .add(R.id.fragmentcontainer,fragment,"register")
                        .addToBackStack(null)
                        .remove(this)
                        .commit()

            }
            else
                Toast.makeText(requireActivity(),"your password is incorrect!",Toast.LENGTH_SHORT).show()
        })


    }

    private fun JudgeLogin() {
        var myAccount=accountEdit.text.toString()
        var myPassword=passEdit.text.toString()
        var ans:Boolean=false

        GlobalScope.launch(Dispatchers.IO) {
            loginVM.loginEqual.postValue(dataViewModel.isEqual(myAccount,myPassword))
            // 终于成功了，通过后台线程更新值
        }


    }







}